/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.booleanExpression;

import java.util.Iterator;

/**
 * A Boolean expression of the form (&phi; &and; &psi;).
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 */
public class Conjunction extends BooleanExpression
{
	/**
	 * The default symbol that is used as conjunction symbol. 
	 */
	public static final String CONJUNCTION_SYMBOL = "&&";

	/*
	 * Left side of the conjunction.
	 */
	private BooleanExpression phi;
	
	/*
	 * Right side of the conjunction.
	 */
	private BooleanExpression psi;
	
	/*
	 * The complete size of this expression. 
	 */
	private int size;
	
	/**
	 * Creates a new conjunction given two other expressions, so it creates the expression
	 * (&phi; &and; &psi;).
	 * 
	 * @param phi Left side of the conjunction.
	 * @param psi Right side of the conjunction.
	 */
	public Conjunction(BooleanExpression phi, BooleanExpression psi)
	{
		this.phi = phi;
		this.psi = psi;
		this.size = phi.completeSize() + psi.completeSize() + 1;
	}
	
	@Override
	public boolean getTruthValue(Assignment alpha)
	{
		return phi.getTruthValue(alpha) && psi.getTruthValue(alpha);
	}

	/**
	 * Return the left side of the conjunction.
	 * 
	 * @return Left side of the conjunction.
	 */
	public BooleanExpression getPhi()
	{
		return phi;
	}

	/**
	 * Return the right side of the conjunction.
	 * 
	 * @return Right side of the conjunction.
	 */
	public BooleanExpression getPsi()
	{
		return psi;
	}
	
	/**
	 * The sum of the hash codes of &phi; and &psi;.
	 */
	@Override
	public int hashCode()
	{
		return phi.hashCode() + psi.hashCode();
	}
	
	/**
	 * Returns the String <code>"(phi.toString() + " " + conjunctionSymbol + " " + psi.toString() + ")"</code>.
	 * 
	 * @param conjunctionSymbol The symbol that is placed between &phi; and &psi;.
	 * 
	 * @return A String representation of this conjunction.
	 */
	public String toString(String conjunctionSymbol)
	{
		return "(" + phi.toString() + " " + conjunctionSymbol + " " + psi.toString() + ")";
	}
	
	/**
	 * The same as {@link #toString(String)} with the default Symbol {@link #CONJUNCTION_SYMBOL},
	 * that is {@value #CONJUNCTION_SYMBOL}.
	 */
	@Override
	public String toString()
	{
		return toString(CONJUNCTION_SYMBOL);
	}
	
	/**
	 * Returns <code>true</code> if other is also a conjunction and has the same left-hand side
	 * and same right-hand side (again tested with the <code>equals</code> method).
	 */
	@Override
	public boolean equals(Object other)
	{
		if(other == null || !(other instanceof Conjunction))
			return false;
		Conjunction otherConj = (Conjunction)other;
		return getPhi().equals(otherConj.getPhi()) && getPsi().equals(otherConj.getPsi());
	}
	
	@Override
	public BooleanFunction getFunction()
	{
		return new BooleanFunction()
		{

			@Override
			public boolean getValue(Iterator<Boolean> inputPoint)
			{
				boolean first, second;
				first = inputPoint.next();
				second = inputPoint.next();
				return first && second;
			}

			@Override
			public int getDimension()
			{
				return 2;
			}
			
		};
	}
	
	@Override
	public int getArity()
	{
		return 2;
	}

	@Override
	public Iterator<BooleanExpression> iterator()
	{
		return new TwoElementIterator(phi, psi);
	}

	@Override
	public int completeSize()
	{
		return size;
	}

}
